import 'package:flutter/material.dart';
import 'package:my_app/pages/Index.dart';
import 'package:my_app/pages/MemberCircle.dart';
import 'package:my_app/pages/UserCentre.dart';
import 'package:my_app/pages/SkinTest.dart';
import 'package:my_app/pages/Consultation.dart';

// 创建一个 带有状态的 Widget view
class NavigationView extends StatefulWidget {

  //  固定的写法
  @override
  State<StatefulWidget> createState()  => new _NavigationViewState();
}

// 要让主页面 Index 支持动效，要在它的定义中附加mixin类型的对象TickerProviderStateMixin
class _NavigationViewState extends State<NavigationView> with TickerProviderStateMixin{

  int _currentIndex = 0;    // 当前界面的索引值
  String aiImg = 'images/camera.png';
  PageController pageController;

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: this._currentIndex);
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }

  //修改bottomNavigationBar的点击事件
  void navigationTap(int index) {
    print('index');
    if (index != 2) {
      setState(() {
        this.aiImg = 'images/camera.png';
      });
    }
    pageController.animateToPage(index,
        duration: const Duration(milliseconds: 300), curve: Curves.ease);
  }

    //添加图片的点击事件
  void onAiImgTap() {
    setState(() {
      this._currentIndex = 2;
      this.aiImg = 'images/camera.png';
      navigationTap(2);
    });
  }

  void onPageChanged(int page) {
    print('page');
    setState(() {
      this._currentIndex = page;
    });
  }

  @override
  Widget build(BuildContext context) {
    // 声明定义一个 底部导航的工具栏
    final bottomNavigationBar = Stack(
      children: <Widget>[
        Scaffold(
          body:PageView(
            physics: NeverScrollableScrollPhysics(),
            children: <Widget>[
              new Index(),
              new MemberCircle(),
              new SkinTest(),
              new Consultation(),
              new UserCentre()
            ],
            controller: pageController,
            onPageChanged: onPageChanged,
          ),
          bottomNavigationBar: BottomNavigationBar(
            type:BottomNavigationBarType.fixed,
            items: [
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('禾粉圈')),
              BottomNavigationBarItem(icon: Icon(Icons.assessment), title: Text('')),
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('咨询')),
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('我的')),
            ],
            onTap: navigationTap,
            currentIndex: _currentIndex
          ),
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child:Padding(
            padding: const EdgeInsets.only(bottom: 20.0),
            child: FloatingActionButton(
              backgroundColor:Color(0xf9f9f9f9),
              child: new Image.asset(
                aiImg,
                width: 36.0,
                height: 36.0,
              ),
              onPressed: onAiImgTap
            )
          )
        )
      ],
    );

    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: new Scaffold(
        backgroundColor:Colors.black,
        bottomNavigationBar: bottomNavigationBar,   // 底部工具栏
      ),
      theme: new ThemeData(
        primarySwatch: Colors.green,   // 设置主题颜色
      ),
    );
  }
}
